package com.duo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duo.dto.CategoryDTO;
import com.duo.dto.CategoryUpdateDTO;
import com.duo.entity.Category;
import com.duo.entity.Dish;
import com.duo.enums.errorcode.CategoryCode;
import com.duo.exception.BaseException;
import com.duo.mapper.CategoryMapper;
import com.duo.mapper.DishMapper;
import com.duo.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Xrx
 * @Description:
 * @CreateTime: 2023/7/6 18:46
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        baseMapper.insert(category);
        Category result = this.getOne(new LambdaQueryWrapper<Category>().eq(Category::getName, category.getName()));
        log.info("新增分类成功：{}", result);
    }

    /**
     * 根据分类名称删除分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        log.info("删除分类：id: {}", id);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId, id);
        Category category = baseMapper.selectOne(wrapper);
        if (ObjectUtil.isNull(category)) {
            log.info("分类不存在");
            throw new BaseException(CategoryCode.CATEGORY_NOT_FOUND);
        }
        baseMapper.deleteById(category.getId());
        log.info("删除分类成功");
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CategoryUpdateDTO categoryDTO) {
        log.info("修改分类：{}", categoryDTO);
        Long id = categoryDTO.getId();
        String name = categoryDTO.getName();
        Integer type = categoryDTO.getType();
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId, id);
        Category category = baseMapper.selectOne(wrapper);
        if (ObjectUtil.isNull(category)) {
            log.info("分类不存在：{}", name);
            throw new BaseException(CategoryCode.CATEGORY_NOT_FOUND);
        }
        // 查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
        Long count = dishMapper.selectCount(dishWrapper);
        if (count > 0) {
            throw new BaseException(CategoryCode.CATEGORY_HAS_RELATION);
        }
        if (ObjectUtil.isNotNull(name)) {
            category.setName(name);
        }
        if (ObjectUtil.isNotNull(type)) {
            category.setType(type);
        }
        baseMapper.updateById(category);
        log.info("修改分类成功");
    }


}
